package com.evon.yardmanagement.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.evon.yardmanagement.exception.YardManagementException;
import com.evon.yardmanagement.exception.YardManagementException.ExceptionType;
import com.evon.yardmanagement.model.ParkingSlot;

/**
 * A data access object (DAO) providing persistence and search support for
 * ParkingSlot entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.abc.yardmanagement.dao.ParkingSlot
 * @author MyEclipse Persistence Tools
 */

public class ParkingSlotDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ParkingSlotDAO.class);
	// property constants
	public static final String SLOT_NUMBER = "slotNumber";
	public static final String SLOT_NAME = "slotName";
	public static final String PRIORITY = "priority";
	public static final String OCCUPIED = "occupied";
	public static final String HSC_ID = "hscId";
	public static final String HSU_ID = "hsuId";

	@Override
	public Class getQueryClass() {
		// TODO Auto-generated method stub
		return ParkingSlot.class;
	}
	
	public void markParkingSlotOccupied(Integer parkingSlotId){
		
		ParkingSlot slot = (ParkingSlot) getSession().load(getQueryClass(),
				parkingSlotId);
		slot.setOccupied(1);
		save(slot);
		
	}

	public List findBySlotNumber(Object slotNumber) {
		return findByProperty(SLOT_NUMBER, slotNumber);
	}

	public List findBySlotName(Object slotName) {
		return findByProperty(SLOT_NAME, slotName);
	}

	public List findByPriority(Object priority) {
		return findByProperty(PRIORITY, priority);
	}

	public List findByOccupied(Object occupied) {
		return findByProperty(OCCUPIED, occupied);
	}

	public List findByHscId(Object hscId) {
		return findByProperty(HSC_ID, hscId);
	}

	public List findByHsuId(Object hsuId) {
		return findByProperty(HSU_ID, hsuId);
	}

	public void markParkingSlotUnOccupied(int slotId) {
		ParkingSlot slot = (ParkingSlot) getSession().load(getQueryClass(),
				slotId);
		slot.setOccupied(0);
		save(slot);
		
	}
	
	public void setActive(int yardId){
		try {
		Transaction transaction = getSession().beginTransaction();
		String stringQuery = "UPDATE ParkingSlot SET isActive = 0 WHERE section.yard.id = :yardId";
		Query query = getSession().createQuery(stringQuery);
		query.setInteger("yardId", yardId);
		query.executeUpdate();
		transaction.commit();
		} catch (Exception re) {
			log.error("Activate section by yard id failed", re);
			throw new YardManagementException(re, ExceptionType.RECOVERABLE);
		}
		
	}
	
	public void setDeActive(int yardId){
		try {
			Transaction transaction = getSession().beginTransaction();
			String stringQuery = "UPDATE ParkingSlot ps join ps.section section join section.yard yard SET isActive = 0 WHERE syard.id = :yardId";
			Query query = getSession().createQuery(stringQuery);
			query.setInteger("yardId", yardId);
			query.executeUpdate();
			transaction.commit();
		} catch (Exception re) {
			log.error("Activate section by yard id failed", re);
			throw new YardManagementException(re, ExceptionType.RECOVERABLE);
		}

		/*try {
			Criteria criteria = getSession().createCriteria(getQueryClass());
			criteria.createAlias("section", "section").createAlias("yard", ""yard);
			criteria.add(Restrictions.eq("yard.id", yardId));


		} catch (HibernateException re) {
			log.error("get failed", re);
			throw new YardManagementException(re, ExceptionType.RECOVERABLE);
		} catch (RuntimeException rex) {
			log.error("get failed", rex);
			throw new YardManagementException(rex, ExceptionType.RECOVERABLE);
		}*/
	}
}